package com.tanhua.manage.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.manage.enums.StateEnum;
import com.tanhua.manage.enums.TopStateEnum;
import com.tanhua.manage.mapper.PublishInfoMapper;
import com.tanhua.manage.mapper.UserInfoMapper;
import com.tanhua.manage.pojo.Publish;
import com.tanhua.manage.pojo.PublishInfo;
import com.tanhua.manage.pojo.UserInfo;
import com.tanhua.manage.vo.PageInfo;
import com.tanhua.manage.vo.PublishDetailsVo;
import com.tanhua.manage.vo.PublishInfoVo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PublishApiImpl implements PublishApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private PublishInfoMapper publishInfoMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public PageInfo<PublishInfoVo> queryPublishInfo(Integer page, Integer pageSize, String sortProrerties,
                                                    ObjectId publishId, Integer userId, String startDate, String endDate) {

        PageInfo<PublishInfoVo> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.asc("created")));
        Query query = new Query().with(pageRequest);
        List<Publish> publishList = this.mongoTemplate.find(query, Publish.class);

        List<Object> publishIds = CollUtil.getFieldValues(publishList, "id");
        ArrayList<String> stringIds = new ArrayList<>();
        for (Object publishId2 : publishIds) {
            String stringId = StrUtil.toString(publishId2);
            stringIds.add(stringId);
        }
        QueryWrapper<PublishInfo> wrapper = new QueryWrapper<>();

        wrapper.in("publish_id", stringIds);

        //根据字段检索
        //   圈子id
        if (ObjectUtil.isNotEmpty(publishId)) {
            wrapper.eq("publish_id", publishId);
        }


        //  用户id
        if (ObjectUtil.isNotEmpty(userId)) {
            wrapper.eq("user_id", sortProrerties);
        }

        //   起始时间
        if (ObjectUtil.isNotEmpty(startDate)) {

            DateTime startTime = DateUtil.parse(startDate, "yyyy-MM-dd HH:mm:ss");
            DateTime endTime = DateUtil.parse(endDate, "yyyy-MM-dd HH:mm:ss");
            long start = startTime.getTime();
            long end = endTime.getTime();
            wrapper.lt("create_date", start);
            wrapper.gt("create_date", end);

        }


        List<PublishInfo> publishInfoList = this.publishInfoMapper.selectList(wrapper);

        // 查询头像昵称等信息
        QueryWrapper<UserInfo> userWrapper = new QueryWrapper<>();

        //   用户昵称筛选
        if (StrUtil.isNotEmpty(sortProrerties)) {
            userWrapper.eq("nick_name", sortProrerties);
        }
        List<UserInfo> userInfoList = this.userInfoMapper.selectList(userWrapper);


        ArrayList<PublishInfoVo> publishInfoVos = new ArrayList<>();
        //  MongoDB中的数据
        for (Publish publish : publishList) {
            for (PublishInfo publishInfo : publishInfoList) {
                PublishInfoVo publishInfoVo = new PublishInfoVo();
                if (StrUtil.equals(publish.getId().toHexString(), publishInfo.getPublishId())) {
                    publishInfoVo.setText(publish.getText());
                    publishInfoVo.setCommentCount(publishInfo.getCommentCount());
                    publishInfoVo.setLikeCount(publishInfo.getLikeCount());
                    publishInfoVo.setForwardingCount(publishInfo.getForwardingCount());
                    publishInfoVo.setPublishId(publish.getId());
                    publishInfoVo.setCreateDate(publishInfo.getCreateDate());
                    publishInfoVo.setReportCount(publishInfo.getReportCount());
                    publishInfoVo.setState(Convert.toEnum(StateEnum.class,publishInfo.getState()));
                    publishInfoVo.setUserId(publishInfo.getUserId());
                    publishInfoVo.setMedias(Convert.toStrArray(publish.getMedias()));
                    for (UserInfo userInfo : userInfoList) {
                        if (publishInfo.getUserId().equals(userInfo.getUserId())) {
                            publishInfoVo.setNickName(userInfo.getNickName());
                            publishInfoVo.setUserLogo(userInfo.getLogo());
                        }

                    }

                    publishInfoVos.add(publishInfoVo);
                }
            }
        }
        pageInfo.setRecords(publishInfoVos);


        return pageInfo;
    }


    @Override
    public PublishDetailsVo queryPublishDetails(String publishId) {

        if (ObjectUtil.isNotEmpty(publishId)) {
            PublishDetailsVo publishDetailsVo = new PublishDetailsVo();

            publishDetailsVo.setPublishId(new ObjectId(publishId));

            Publish publish = this.mongoTemplate.findOne(Query.query(Criteria.where("id").is(publishId)), Publish.class);
            publishDetailsVo.setCreateDate(publish.getCreated());
            publishDetailsVo.setMedias(Convert.toStrArray(publish.getMedias()));
            publishDetailsVo.setText(publish.getText());

            QueryWrapper<PublishInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("publish_id", publishId);
            PublishInfo publishInfo = this.publishInfoMapper.selectOne(wrapper);
            publishDetailsVo.setCommentCount(publishInfo.getCommentCount());
            publishDetailsVo.setForwardingCount(publishInfo.getForwardingCount());
            publishDetailsVo.setReportCount(publishInfo.getReportCount());
            publishDetailsVo.setLikeCount(publishInfo.getLikeCount());
            publishDetailsVo.setState(Convert.toEnum(StateEnum.class,publishInfo.getState()));
            publishDetailsVo.setTopState(Convert.toEnum(TopStateEnum.class,publishInfo.getTopState()));
            publishDetailsVo.setUserId(publishInfo.getUserId());


            QueryWrapper<UserInfo> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("user_id", publishInfo.getUserId());
            UserInfo userInfo = this.userInfoMapper.selectOne(wrapper2);
            publishDetailsVo.setNickName(userInfo.getNickName());
            publishDetailsVo.setUserLogo(userInfo.getLogo());


            return publishDetailsVo;
        }

        return null;

    }


}